#include "iConfig.hpp"

#include <glf/core/timer.h>

#include <time.h>

#if GLF_PLATFORM_IPHONE
	#include "timer_iphone_.hpp"
#elif GLF_PLATFORM_MACOSX
	#include "timer_posix_.hpp"
#else
	#include GLFi_HEADER_API(timer, GLF_API_TIME)
#endif


namespace glf {

int64 sTimerTickOffset = 0;

void OffsetTimer(int64 ticks) {
	sTimerTickOffset += ticks;
}
  
inline double GetSecondsDouble(int64 offset = 0) {
	return double(GetTicks() - offset) / double(GetTicksPerSecond());
}

int64 GetMilliseconds() {
	return int64(GetSecondsDouble() * 1000);
}

int64 GetMicroseconds() {
	return int64(GetSecondsDouble() * 1000 * 1000);
}
int64 GetNanoseconds() {
	return int64(GetSecondsDouble() * 1000 * 1000 * 1000);
}

int64 GetRunningTimeMs() {
	return int64(GetSecondsDouble(sTimerTickOffset) * 1000);
}

int64 GetRunningTimeUs() {
	return int64(GetSecondsDouble(sTimerTickOffset) * 1000 * 1000);
}

int64 GetRunningTimeNs() {
	return int64(GetSecondsDouble(sTimerTickOffset) * 1000 * 1000 * 1000);
}

void GetCalendar(Calendar& calendar) {

	time_t now = time(0);
	struct tm* ts = localtime(&now);
	
	calendar.second		= ts->tm_sec;
	calendar.minute		= ts->tm_min;
	calendar.hour		= ts->tm_hour;
	calendar.monthDay	= ts->tm_mday;
	calendar.month		= ts->tm_mon;
	calendar.weekDay	= ts->tm_wday;
	calendar.day		= ts->tm_yday;
	calendar.year		= 1900 + ts->tm_year;
	calendar.millisecond= 0;
}

} //namespace glf

